// Copyright 2015-2024 Beken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "vad_core.h"
#include "vad.h"

#define VAD_BIG_ENDIAN          0
#define VAD_UNDERSAMPLED        0

//sum是检测到声音的'大小'，sum2是阈值
int vad(short samples[], int len)
{
    #if VAD_BIG_ENDIAN
    unsigned char *p;
    unsigned char temp;
    #endif
    long  sum = 0, sum2 = 0;

    for(int i = 0; i < len; i++)
    {
        #if VAD_BIG_ENDIAN
        p = &samples[i];
        temp = p[0];
        p[0] = p[1];
        p[1] = temp;
        #endif

        #if VAD_UNDERSAMPLED
        if(1 == (i & 0x01))
        #endif
        {
            sum += (samples[i] * samples[i]);
            sum2 += (POWER_THRESHOLD_VAL);
        }
    }

    if (sum > sum2)
        return FRAME_TYPE_SPEECH;
    else
        return FRAME_TYPE_NOISE;
}
// eof

